package main

import (
	// "context"
	"flag"
	"log"
	"net/http"

	// "os"

	// dbPack "local/messenger2/pkg/serverdb"

	_ "github.com/golang-migrate/migrate/v4/database/postgres"
	_ "github.com/golang-migrate/migrate/v4/source/file"
	wsapi "notabug.org/kaya613/huber-go-server/api/websocket"
)

var addr = flag.String("addr", "127.0.0.1:8000", "listening address")

func main() {
	flag.Parse()
	log.SetFlags(3) // 0 - disable, 3 - date and time
	// Upgrading database to latest version
	// log.Println("migrating")
	// err := dbPack.Upgrade("file:///home/stripwire/go/src/local/messenger2/misc/server/db/migrate/versions/", os.Getenv("DATABASE_URL"))
	// if err != nil {
	// 	log.Println("migrate: ", err)
	// }

	// conn := dbPack.Connect(os.Getenv("DATABASE_URL"))
	// defer conn.Close(context.Background())

	log.Print("starting up http")
	http.HandleFunc("/msg", wsapi.NewConnection)
	log.Print("listen on ", *addr)
	log.Fatal(http.ListenAndServeTLS(*addr, "./local/cert/cert.crt", "./local/cert/cert.key", nil))
}

// var upgrader = websocket.Upgrader{
// 	CheckOrigin: func(r *http.Request) bool { return true },
// }

// func msg(w http.ResponseWriter, r *http.Request) {
// 	c, err := upgrader.Upgrade(w, r, nil)
// 	if err != nil {
// 		log.Print("upgrade: ", err)
// 		return
// 	}
// 	defer c.Close()

// 	log.Println("new client:", r.RemoteAddr)

// 	for {
// 		mt, message, err := c.ReadMessage()
// 		if err != nil {
// 			log.Print("reader: ", err)
// 			break
// 		}
// 		log.Print("message:", message)
// 		err = c.WriteMessage(mt, message)
// 		if err != nil {
// 			log.Print("write: ", err)
// 			break
// 		}
// 	}
// }
